home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / Kibitz / Window.c < prev   
Encoding:
C/C++ Source or Header  |  1991-02-21  |  5.5 KB  |  232 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:     Kibitz
  5. ** File:        window.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  19. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  21.  
  22. #ifndef __ERRORS__
  23. #include <Errors.h>
  24. #endif
  25.  
  26. #ifndef __SYSEQU__
  27. #include <SysEqu.h>
  28. #endif
  29.  
  30. #ifndef __UTILITIES__
  31. #include <Utilities.h>
  32. #endif
  33.  
  34.  
  35.  
  36. /*****************************************************************************/
  37.  
  38.  
  39.  
  40. extern short    gPrintPage;        /* Non-zero means we are printing. */
  41.  
  42.  
  43.  
  44. /*****************************************************************************/
  45. /*****************************************************************************/
  46.  
  47.  
  48.  
  49. /* This function creates a new application window.  An application window
  50. ** contains a document which is referenced by a handle in the refCon field.
  51. */
  52.  
  53. #pragma segment Window
  54. OSErr    AppNewWindow(FileRecHndl frHndl, WindowPtr *retWindow)
  55. {
  56.     WindowPtr        oldPort, window;
  57.     OSErr            err;
  58.  
  59.     /* We will allocate our own window storage instead of letting the Window
  60.     ** Manager do it because GetNewWindow may load in temp. resources before
  61.     ** making the NewPtr call, and this can lead to heap fragmentation.
  62.     */
  63.  
  64.     GetPort(&oldPort);
  65.  
  66.     err = memFullErr;        /* Assume that we will fail.  Good attitude. */
  67.  
  68.     if (window = GetStaggeredWindow(rWindow, nil, FrontWindow(), (WindowPtr)-1, true)) {
  69.         (*frHndl)->fileState.window = window;
  70.         SetWRefCon(window, (long)frHndl);
  71.         AppNewWindowTitle(window);
  72.         if (!(err = AppNewWindowControls(frHndl, window))) {
  73.             if (gPrintPage)
  74.                 MoveWindow(window, 16384, 16384, true);
  75.                     /* So the window can be hidden while printing, yet
  76.                     ** PrintMonitor can get the document name. */
  77.             ShowWindow(window);
  78.             if (gPrintPage)
  79.                 MoveWindow(window, 16384, 16384, true);
  80.                     /* Moving invisible windows to the front doesn't always
  81.                     ** get them to the front.  Now that it is visible, moving
  82.                     ** it will definitely get it to the front. */
  83.         }
  84.     }
  85.  
  86.     SetPort(oldPort);
  87.     if (retWindow) *retWindow = window;
  88.  
  89.     return(err);
  90. }
  91.  
  92.  
  93.  
  94. /*****************************************************************************/
  95.  
  96.  
  97.  
  98. /* This function updates the window title to reflect the new document name.
  99. ** The new document name is stored in the fileState portion of the document.
  100. ** This is automatically set to 'Untitled # N' for new documents, and is
  101. ** updated when a user does a save-as.
  102. */
  103.  
  104. #pragma segment Window
  105. void    AppNewWindowTitle(WindowPtr window)
  106. {
  107.     FileRecHndl    frHndl;
  108.     Str255        wTitle;
  109.  
  110.     if (frHndl = (FileRecHndl)GetWRefCon(window)) {
  111.         pstrcpy(wTitle, (*frHndl)->fileState.fss.name);
  112.         SetWTitle(window, wTitle);
  113.     }
  114. }
  115.  
  116.  
  117.  
  118. /*****************************************************************************/
  119.  
  120.  
  121.  
  122. /* This function returns the state of the window's document.  If the document
  123. ** is dirty, then true is returned.  If the document is clean, or the window
  124. ** has no document, then false is returned.
  125. */
  126.  
  127. #pragma segment Window
  128. Boolean    AppWindowDirty(WindowPtr window)
  129. {
  130.     FileRecHndl    frHndl;
  131.  
  132.     if (frHndl = (FileRecHndl)GetWRefCon(window))
  133.         return(AppDocumentDirty(frHndl));
  134.  
  135.     return(false);
  136. }
  137.  
  138.  
  139.  
  140. /*****************************************************************************/
  141.  
  142.  
  143.  
  144. /* Close all the windows.  This is called prior to quitting the application.
  145. ** This function returns true if all windows were closed.  The user may decide
  146. ** to abort a save, thus stopping the closing of the windows.  If the user
  147. ** does this, false will be returned, indicating that all windows were not
  148. ** closed after all.
  149. */
  150.  
  151. #pragma segment Window
  152. Boolean    CloseAllWindows(void)
  153. {
  154.     WindowPtr    window;
  155.     
  156.     while (window = *(WindowPtr *)WindowList) {
  157.  
  158.         /* While we have a front window, try closing it. */
  159.  
  160.         if (!CloseOneWindow(window, iQuit)) return(false);
  161.             /* When CloseOneWindow returns false, this means that the window
  162.             ** didn't close.  The only cause of this is if the window had a
  163.             ** document that needed saving, and the user cancelled the save.
  164.             ** If the window succeeded in getting closed, then we are
  165.             ** returned true.  Either way, we return the result.
  166.             */
  167.     }
  168.  
  169.     return(true);
  170. }
  171.  
  172.  
  173.  
  174. /*****************************************************************************/
  175.  
  176.  
  177.  
  178. /* Closes one window.  This window may be an application window, or it may be
  179. ** a system window.  If it is an application window, it may have a document
  180. ** that needs saving.
  181. */
  182.  
  183. #pragma segment Window
  184. Boolean    CloseOneWindow(WindowPtr window, short saveMode)
  185. {
  186.     FileRecHndl    frHndl;
  187.     OSErr        err;
  188.  
  189.     if (IsAppWindow(window)) {
  190.         /* First, if the window is an application window, try saving
  191.         ** the document.  Remember that the user may cancel the save.
  192.         */
  193.  
  194.         if (frHndl = (FileRecHndl)GetWRefCon(window)) {
  195.  
  196.             err = AppSaveDocument(frHndl, window, saveMode);
  197.             if (err) {
  198.                 if (err != userCanceledErr)
  199.                     Alert(rErrorAlert, nil);
  200.                 return(false);
  201.             }        /* Stop closing windows on error or user cancel. */
  202.  
  203.             SetOpponentType(frHndl, kOnePlayer);
  204.             AppDisposeDocument(frHndl);
  205.                 /* The document is saved, or the user doesn't care about
  206.                 ** that document, so dispose of the document.
  207.                 */
  208.         }
  209.     }
  210.     DisposeAnyWindow(window);
  211.     return(true);
  212. }
  213.  
  214.  
  215.  
  216. /*****************************************************************************/
  217.  
  218.  
  219.  
  220. #pragma segment Window
  221. WindowPtr    SetFilePort(FileRecHndl frHndl)
  222. {
  223.     WindowPtr    oldPort;
  224.  
  225.     GetPort(&oldPort);
  226.     SetPort((*frHndl)->fileState.window);
  227.     return(oldPort);
  228. }
  229.  
  230.  
  231.  
  232.